package com.useeinfo.oa.modules.security.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.security.entity.SystemModule;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("SystemModuleDao")
public class SystemModuleDao implements CrudDao<SystemModule> {
    private final static Logger logger = LoggerFactory.getLogger(SystemAuthorityDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    // ******************************************************************************
    // ******************************* 代码自动生成开始 *******************************
    // ******************************************************************************

    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {

        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String moduleName = queryHash.get("moduleName");
        if (StringUtils.isNotBlank(moduleName)) {
            conditionHash.put("moduleName = ?{paramIndex} ", moduleName);
        }
        String moduleId = queryHash.get("moduleId");
        if (StringUtils.isNotBlank(moduleId)) {
            conditionHash.put("id = ?{paramIndex} ", StringConverters.ToLong(moduleId));
        }
        String moduleNameLike = queryHash.get("moduleNameLike");
        if (StringUtils.isNotBlank(moduleNameLike)) {
            conditionHash.put("moduleName like ?{paramIndex} ", "%" + moduleNameLike + "%");
        }
        String magicLike = queryHash.get("magicLike");
        if (StringUtils.isNotBlank(magicLike)) {
            conditionHash.put("moduleName like ?{paramIndex} ", "%" + magicLike + "%");
        }
        String magicId = queryHash.get("magicId");
        if (StringUtils.isNotBlank(magicId)) {
            conditionHash.put("id != ?{paramIndex} ", StringConverters.ToLong(magicId));
        }
        String applicationModuleId = queryHash.get("applicationModuleId");
        if (StringUtils.isNotBlank(applicationModuleId)) {
            conditionHash.put("applicationModuleId = ?{paramIndex} ", StringConverters.ToLong(applicationModuleId));
        }


        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from SystemModule s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<SystemModule> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from SystemModule s ", "order by s.id desc ");

        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<SystemModule> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, SystemModule.class);

        return queryParam.findPageList(typedQuery);
    }

    @Override
    public SystemModule findModel(Long id) {
        return entityManager.find(SystemModule.class, id);
    }

    @Override
    public Integer add(SystemModule model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("SystemModuleDaoImpl添加SystemModule成功！");
        return 1;
    }

    @Override
    public Integer update(SystemModule model) {
        SystemModule systemModule = entityManager.find(SystemModule.class, model.getId());
        systemModule.setModuleName(model.getModuleName());
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        SystemModule systemModule = entityManager.find(SystemModule.class, id);
        entityManager.remove(systemModule);
        return 1;
    }

    @Override
    public Integer deleteLogic(SystemModule model) {
        return null;
    }
}
